package com.fingersoft.feature.push.huawei

import android.util.Log
import cn.fingersoft.util.BuildConfigUtil
import com.fingersoft.business.BusinessContext
import com.fingersoft.common.ICallback
import com.huawei.hms.push.HmsMessageService
import com.huawei.hms.push.RemoteMessage


/**
 * @author：mac Email：jooperge@163.com
 * 描述：
 * 修改历史:
 *
 *
 * 创建于： 2020/8/11
 */
open class MHmsMessageService : HmsMessageService() {
    private val TAG = "MHmsMessageService"

    override fun onNewToken(s: String) {
        super.onNewToken(s)
        Log.e(TAG, "____________onNewToken \n $s")
        BusinessContext.jpushProvider?.onNewToken(s)
        s?.let {
            if (BuildConfigUtil.getBoolean("useJpush")) {//开启极光的情况下，推送类型不能在门户后台注册为 HUAWEI
                return
            }
            registerDevice(it)
        }
    }

    private fun registerDevice(token: String) {
        val extra = HashMap<String, String>()
        extra["pushType"] = "HUAWEI"
        extra["pushToken"] = token
        BusinessContext.device?.registerDevice(extra, object : ICallback<String>(String::class.java) {
            override fun onSuccess(data: String?) {
                super.onSuccess(data)
                data?.let {
                    Log.e(TAG, it)
                }
            }
        })
    }

    override fun onMessageReceived(remoteMessage: RemoteMessage?) {
        BusinessContext.jpushProvider?.onMessageReceived(remoteMessage)
    }

    override fun onMessageSent(s: String?) {
        BusinessContext.jpushProvider?.onMessageSent(s)
    }

    override fun onSendError(s: String?, e: Exception?) {
        BusinessContext.jpushProvider?.onSendError(s, e)
    }

    override fun onDeletedMessages() {
        BusinessContext.jpushProvider?.onDeletedMessages()
    }
}
